package com.ld.businessCard.action;

import com.alibaba.excel.EasyExcel;
import com.ld.businessCard.entity.Card;
import com.ld.businessCard.entity.User;
import com.ld.businessCard.listener.MyUploadDataListener;
import com.ld.businessCard.service.CardService;
import com.ld.businessCard.service.impl.CardServiceImpl;
import com.ld.businessCard.utils.ExcelUtil;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
public class CardAction implements ModelDriven<Card> {
	//excel模板下载
	private InputStream inputStream;
	private String filename;
	//excel文件上传
	private File uploadFile;
	private String uploadFileFileName;
	public Card card;
	private CardService cardService = new CardServiceImpl();

	public Card getModel() {
		card = new Card();
		return card;
	}
	public String add(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if (card.getUserId() == null) {
			card.setUserId(user.getId());
		}
		card.setStatus(0);
		int i = cardService.addCard(card);
		if(i == 1){
			session.setAttribute("message","添加成功");
		}else {
			session.setAttribute("message","添加失败");
		}
		return "card-add";
	}
	public String recycle(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		int i = cardService.recycleCard(card.getId());
		if(i == 1){
			session.setAttribute("message","放入回收站成功");
		}else {
			session.setAttribute("message","放入回收站失败");
		}
		return "card-recycle";
	}
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		int i = cardService.deleteCardById(card.getId());
		if(i == 1){
			session.setAttribute("message","删除成功");
		}else {
			session.setAttribute("message","删除失败");
		}
		return "card-delete";
	}
	public String recover(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		int i = cardService.recoverCard(card.getId());
		if(i == 1){
			session.setAttribute("message","恢复成功");
		}else {
			session.setAttribute("message","恢复失败");
		}
		return "card-recover";
	}
	public String change(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Card card0 = cardService.getCardById(card.getId());
		card.setStatus(card0.getStatus());
		card.setUserId(card0.getUserId());
		int i = cardService.changeCard(card);
		if(i == 1){
			session.setAttribute("message","修改成功");
		}else {
			session.setAttribute("message","修改失败");
		}
		return "card-change";
	}
	//导出excel
	public void downLoadCards(){
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			HttpServletResponse resp = ServletActionContext.getResponse();
			User user = (User)session.getAttribute("user");
			List<Card> cards;
			if(user.getStatus() == 0){
				cards = cardService.getMyCards(user.getId());
			}else {
				cards = cardService.getAllCards();
			}
			resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			resp.setCharacterEncoding("utf-8");
			// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
			String fileName = URLEncoder.encode("名片信息", "UTF-8").replaceAll("\\+", "%20");
			resp.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
			EasyExcel.write(resp.getOutputStream(), Card.class).sheet("我的名片").doWrite(ExcelUtil.data(cards));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	//导入excel
	public String importExcel(){
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("user");
			if(uploadFile == null) {
				session.setAttribute("message","导入失败");
				return "card-import";
			}
			EasyExcel.read(new FileInputStream(uploadFile), Card.class, new MyUploadDataListener(user)).sheet().doRead();
			session.setAttribute("message","导入成功");
			return "card-import";
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
	public String excelTemplate(){
		return "success";
	}

	public InputStream getInputStream() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("user");
			String filePath;
			if(user.getStatus() == 0){
				filePath = "/home/upload/template.xlsx";
			}else {
				filePath = "/home/upload/template1.xlsx";
			}
			inputStream = new FileInputStream(ServletActionContext.getRequest().getServletContext().getRealPath(filePath));
			return inputStream;
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}


	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}
}
